package com.lbl.flowabledemo02;

import liquibase.pro.packaged.S;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;


@Slf4j
@SpringBootTest
class FlowableTest03 {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;


    @Test
    void flowableTest() {
        log.info("flowableTest...run");
    }

    /**
     * 流程部署操作
     */
    @Test
    void flowableTest01() {
//        log.info("ProcessEngine:{}", processEngine);
//        RepositoryService repositoryService = processEngine.getRepositoryService();
        log.info("repositoryService:{}", repositoryService);
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/基础/Example02.bpmn20.xml")
                .name("任务分配案例——监听器")
                .deploy();
        log.info("deploy.getId = {}", deploy.getId());
    }


    /**
     * 启动流程实例
     */
    @Test
    void startFlow() {
        //在流程定义表中动态维护
        String processId = "Example02:1:116f5e65-6b33-11ef-920f-00ff00c159b7";
        String processKey = "FirstFlow";
        //在启动流程实例的时候我们就可以绑定对应的表达式的值
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("myAssignee2", "zhangsan");
        //1.根据流程定义ID启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processId, variables);
        //2.根据流程定义key启动流程实例
//        ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey(processKey);
    }


    /**
     * 根据用户查询待办信息
     */
    @Test
    void findFlow() {
        // 任务实例操作都是通过 taskService 来实现的
//        TaskService taskService = processEngine.getTaskService();

        //指定查询条件
        List<Task> taskList = taskService.createTaskQuery()
                .taskAssignee("zhangsan")
                .list();
        for (Task task : taskList) {
            log.info("taskList:{}", task.getId());
        }
    }

    /**
     * 任务的审批
     */
    @Test
    void completeTask() {
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("myAssignee1", "lisi");
        //根据任务的ID，完成任务的审批,绑定对应的表达式的值
        taskService.complete("b1cfea39-6b30-11ef-96aa-00ff00c159b7");
    }
}
